package logic

import (
	"context"
	"product/models/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *productrpc.ProductAddRequest) (*productrpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	//var product mysql.Product
	//mysql.DB.Debug().Where("id = ?", product.ID).Find(&product)
	//
	//if product.ID == 0 {
	//	return nil, errors.New("商品已经存在")
	//}
	//if product.Num != 10 {
	//	return nil, errors.New("库存不足")
	//}

	product := mysql.Product{
		ProductName: in.ProductName,
		Price:       float64(in.Price),
		Num:         int(in.Num),
	}
	mysql.DB.Debug().Create(&product)

	return &productrpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
